package com.wchc.controller;

import com.wchc.entities.TUser;
import com.wchc.entities.base.ResultVo;
import com.wchc.service.TUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sparrow-w
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户控制器")
public class TUserController {

    @Resource
    private TUserService tUserService;


    @PostMapping("saveOne")
    @Operation(summary = "保存用户信息")
    public ResultVo saveUser(TUser tUser){
        tUser.setId(null);
        return ResultVo.ok(tUserService.saveOrUpdate(tUser));
    }


    @GetMapping("getOne")
    @Operation(summary = "获取单个用户信息")
    public ResultVo getUser(Integer id){
        return ResultVo.ok(tUserService.getById(id));
    }

    @GetMapping("modify")
    @Operation(summary = "修改用户信息")
    public ResultVo updateUser(TUser user){
        if(Objects.isNull(user)){
            return ResultVo.error();
        }
        if (user.getId()==null || user.getId()==0) {
            return ResultVo.error();
        }
        return ResultVo.ok(tUserService.updateById(user));
    }


}

